<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>栈</title>
</head>
<body>
  <script>
    const _items = Symbol('stackItems')
    class Stack {
      constructor() {
        this[_items] = {}
        this.count = 0
      }
      push(item) {
        this[_items][this.count] = item
        this.count++
      }
      isEmpty() {
        return this.count === 0
      }
      pop() {
        if(this.isEmpty()) {
          return undefined
        }
        this.count--
        let result = this[_items][this.count]
        delete this[_items][this.count]
        return result
      }
      peek() {
        return this[_items][this.count - 1]
      }
      size() {
        return this.count
      }
      clear() {
        while(!this.isEmpty()) {
          this.pop()
        }
      }
      toString() {
        if(this.isEmpty()){
          return ''
        }
        let objString = this[_items][0]
        for(let i = 1; i < this.count; i++) {
          objString = `${objString},${this[_items][i]}`
        }
        return objString
      }
    }
    function decimalToBinary (decimal) {
      const stack = new Stack()
      let number = decimal
      let rem;
      let binaryString = '';
      while(number > 0) {
        rem = Math.floor(number % 2)
        stack.push(rem)
        number = Math.floor(number / 2)
      }
      while(!stack.isEmpty()) {
        binaryString += stack.pop().toString()
      }
      return binaryString
    }
    console.log(decimalToBinary(10))
  </script>
</body>
</html>